package com.yonyou.pmclouds.print.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.view.DisableWrap;
import com.yonyou.pmclouds.print.rmiitf.PrintMaintain;
import com.yonyou.pmclouds.print.util.ParamMapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@Slf4j
@RestController
@RequestMapping(value = {"/print","/outter/print"})
public class PrintController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private PrintMaintain printMaintain;

    @PostMapping(value = "/doPost", produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String doPrintPost(HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            return printMaintain.doPrintPost(paramMap);
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("converter params : " + e.getMessage());
        }
    }

    @GetMapping(value = "/doGet", produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String doPrintGet(HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            return printMaintain.doPrintGet(paramMap);
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("converter params : " + e.getMessage());
        }
    }

    @GetMapping(value = "/design", produces = "application/json;charset=UTF-8")
    public void getPrintDesign(HttpServletResponse response, HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            String html = printMaintain.getPrintDesign(paramMap);
            response.setContentType("text/html; charset=utf-8");
            response.getWriter().print(html);
            response.flushBuffer();
        } catch (IOException e) {
            throw new BusinessException("get design html : " + e.getMessage());
        }
    }

    @GetMapping(value = "/preview", produces = "application/json;charset=UTF-8")
    public void getPrintPreview(HttpServletResponse response, HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            String html = printMaintain.getPrintPreview(paramMap);
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().print(html);
            response.flushBuffer();
        } catch (IOException e) {
            throw new BusinessException("get preview html : " + e.getMessage());
        }
    }

    @RequestMapping(value = {"/getPrintData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @DisableWrap
    public String getPrintData(HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            String realUrl = request.getScheme() + "://" + request.getServerName() + ":" + String.valueOf(request.getServerPort());
            return printMaintain.getPrintData(paramMap, realUrl);
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("get print data : " + e.getMessage());
        }
    }

    @RequestMapping(value = {"/printdelegate"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @DisableWrap
    public String getPrintDelegate(HttpServletRequest request) throws BusinessException{
        try {
            Map<String, String> paramMap = ParamMapUtil.getParameterMap(request.getParameterMap());
            String method = request.getMethod();
            return printMaintain.getPrintDelegate(paramMap, method);
        } catch (Exception e) {
            throw new BusinessException("print delegate : " + e.getMessage());
        }
    }

    /**
     *
     * 输出文档暂时注释掉

    @PostMapping(value = {"/exportFile"}, produces = {"application/json;charset=UTF-8"})
    public HttpServletResponse exportFile(@RequestParam Map<String, Object> paramsMap,
                                          HttpServletResponse response) throws BusinessException{
        String tenantId = RuntimeEnvironment.getTenantId();
        InputStream inputStream = printMaintain.exportFile(paramsMap, tenantId);
        try {
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" +
                    new String(BusinessObjectConsts.DEFAULT_FILE_NAME.getBytes()));
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        }catch (IOException e){
            throw new BusinessException("export file :" + e);
        }
        return response;
    }

     *
     */
}
